package com.lxyblog2030.blog.controller.admin;

import com.lxyblog2030.blog.pojo.User;
import com.lxyblog2030.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.jar.Attributes;

@Controller
public class LoginController {

    @Autowired
    UserService userService;

    @GetMapping("/admin")
    public String loginPage(){
        return "admin/login";
    }


   @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession httpSession, RedirectAttributes redirectAttributes){
        User user = userService.Userlogin(username, password);
        if (user!=null){
            httpSession.setAttribute("user",user);
            return "admin/index";
        }else{
            redirectAttributes.addFlashAttribute("message","用户名或者密码错误");
            return "redirect:/admin";
        }
    }


    @GetMapping("/logout")
    public String loginout(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return "redirect:/admin";
    }
}
